<template>
  <el-container class="home">
    <el-header>
      <el-row>
        <el-col :span="20" class="left"
          ><div class="logo"><img :src="logoImg" alt="logo" /></div>
          <div class="title">XX后台管理系统</div></el-col
        >
        <el-col :span="4">
          <el-button type="primary" round size="mini">{{ username }}</el-button>
          <el-button round size="mini" @click="handleLogout">退出</el-button>
        </el-col>
      </el-row>
    </el-header>
    <el-container>
      <!-- 侧边栏组件处理 -->
      <home-aside :asideList="asideList"></home-aside>
      <el-main>
        <el-card>
          <router-view></router-view>
        </el-card>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// @ is an alias to /src
import HomeAside from '@/views/home/components/HomeAside'

export default {
  name: 'Home',
  components: {
    HomeAside,
  },

  data() {
    return {
      logoImg: require('@/assets/img/logo.png'),
      asideList: [],
      username: '',
    }
  },
  created() {
    this.getAsideList()
    this.username = localStorage.getItem('username') || ''
  },
  methods: {
    handleLogout() {
      // 删除 token，转到 login 页面
      localStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 获取侧边栏菜单数据
    async getAsideList() {
      const {
        data: { data, meta },
      } = await this.$http.get('/menus')

      if (meta.status === 200) {
        this.asideList = data
      } else {
        this.$message.warning(data.meta.msg)
      }
    },
  },
}
</script>

<style lang="less" scoped>
.home {
  height: 100%;

  .el-header {
    background-color: #ffffff;
    color: #333;
    line-height: 60px;
    .left {
      display: flex;

      .logo {
        width: 60px;
        height: 60px;
        display: flex;
        justify-content: center;
        align-items: center;
        img {
          height: 80%;
        }
      }
      .title {
        margin-left: 10px;
        font-weight: 400;
        font-size: 22px;
        text-align: center;
        // letter-spacing: 5px;
      }
    }
  }

  .el-main {
    background-color: #e9eef3;
    border: 1px solid #eeeeee;
    color: #333;
    overflow: auto;
    height: calc(100vh - 60px);
    padding: 10px;
  }
}

// #336699

// #99CCFF

// #ffffff
</style>
